/**
 * @author zhounan 
 * @date 2024年7月9日
 * @version V1.0
 */
package org.zn.etl.job.handler.extend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Component;
import org.zn.datax.common.entity.ColumnInfo;
import org.zn.etl.job.transformer.DataTransformer;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhounan 
 * @date 2024年7月9日
 * @version V1.0
 */
@Component()
@Slf4j
public class CitiyTrans implements DataTransformer {

	
	protected Map<String,String> cityMappping;
	
	protected String[] keys=new String[] {
	"CITY",
	"CITY_ZS",
	"ORG_CITY",
	"INVOICECITY",
	"PERSON_CITY",
	"REGION_CITY",
	"AUDITOR_CITY",
	"ORDINARY_CITY",
	"BUSTYPECITY",
	"BILLRECEIVE_CITY",
	"DATAREGIONCITY",
	"DATA_REGION_CITY",
	"PERSON_CONTACT_CITY"
	};
	

	@PostConstruct
	public void init() {
		
		cityMappping=new HashMap<>();
		cityMappping.put("341400", "340100");
		cityMappping.put("371200", "370100");
		cityMappping.put("542100", "540200");
		cityMappping.put("542200", "540300");
		cityMappping.put("542300", "540400");
		cityMappping.put("542400", "540500");
		cityMappping.put("542500", "540600");
		cityMappping.put("632100", "630200");
		cityMappping.put("652100", "650400");
		cityMappping.put("652200", "650500");
	
		log.info("CitiyTrans init finish, cityMapppings:{} ",cityMappping.size());
	
	}
	
	
	@Override
	public List<Map<String, Object>> transform(List<Map<String, Object>> data,
			List<Pair<ColumnInfo, ColumnInfo>> columnMappingPairs) {
		// TODO Auto-generated method stub
	
		String useK=getUseKey(data.get(0));

		if(useK==null) {
			log.info("CitiyTrans No citiy UseKey");
			return data;
		}
		
		for (Map<String, Object> obj : data) {
			transCity(obj, useK);
		}
			
		return data;
	}
	
	
	protected String getUseKey(	Map<String, Object> simple) {
		
		for (String  k : keys) {
			if(simple.containsKey(k.toLowerCase())||simple.containsKey(k)) {
				return k;
			}
		}
		return null;

	}
	
	
	protected void  transCity(Map<String, Object> obj,String useK) {
		Object cv=obj.get(useK.toLowerCase());
		if(cv!=null) {
			String tv= cityMappping.get(cv.toString());
			if(StringUtils.isNoneEmpty(tv)) {
			obj.put(useK.toLowerCase(), tv);
			}
		}
	}

}
